 package net.go176.backpack.core.compatible;
 
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import net.go176.backpack.Backpack;
 import net.go176.backpack.core.MyDataHandlerGUI;
 import net.go176.backpack.gui.InventoryType;
 import net.go176.backpack.gui.OfflineInvHolder;
 import net.go176.backpack.gui.OnlineInvHolder;
 import net.go176.backpack.util.InvTrans;
 import net.go176.backpack.util.NBTUtil;
 import net.go176.backpack.util.NMSUtil;
 import net.go176.backpack.util.simplenbt.NBTBase;
 import net.go176.backpack.util.simplenbt.NBTCompressedStreamTools;
 import net.go176.backpack.util.simplenbt.NBTNumber;
 import net.go176.backpack.util.simplenbt.NBTTagByte;
 import net.go176.backpack.util.simplenbt.NBTTagCompound;
 import net.go176.backpack.util.simplenbt.NBTTagList;
 import org.bukkit.entity.Entity;
 import org.bukkit.entity.HumanEntity;
 import org.bukkit.entity.Player;
 import org.bukkit.inventory.Inventory;
 import org.bukkit.inventory.ItemStack;
 
 public class VanillaHandler
   extends MyDataHandlerGUI {
   public VanillaHandler(Backpack plugin) {
     super(plugin);
   }
 
   
   public InventoryType getInventoryType() {
     return InventoryType.Inventory;
   }
 
   
   public void saveData(File file, Player player) throws IOException {
     if (!this.cfgMan.multi_Vanilla)
       return; 
     NBTTagCompound from = new NBTTagCompound();
     from.readFromEntity((Entity)player);
     NBTTagCompound to = new NBTTagCompound();
     copyNBTValues(from, to);
     File filetmp = new File(String.valueOf(file.getPath()) + ".tmp");
     FileOutputStream output = new FileOutputStream(filetmp);
     try {
       NBTCompressedStreamTools.write(to, output);
     } finally {
       output.close();
     } 
     if (file.exists())
       file.delete(); 
     filetmp.renameTo(file);
   }
 
   
   public void loadData(File file, Player player) throws IOException {
     if (!this.cfgMan.multi_Vanilla)
       return; 
     NBTTagCompound from = new NBTTagCompound();
     if (file.exists() && file.isFile()) {
       FileInputStream input = new FileInputStream(file);
       try {
         from = NBTCompressedStreamTools.read(input);
       } finally {
         input.close();
       } 
     } 
     NBTTagCompound to = new NBTTagCompound();
     Object nms_player = NMSUtil.getNMSEntity((Entity)player);
     to.readFromNMSEntity(nms_player);
     NMSUtil.resetPlayerEntity(nms_player);
     clearNBTValues(to);
     copyNBTValues(from, to);
     to.writeToNMSEntity(nms_player);
   }
 
 
 
 
 
 
 
 
 
 
   
   public void readOnlineInv(Inventory inv, Player player) {
     OnlineInvHolder holder = (OnlineInvHolder)inv.getHolder();
     boolean isEnderChest = (holder.invType == InventoryType.EnderChest);
     if (isEnderChest) {
       if (holder.hasRead)
         return; 
       holder.hasRead = true;
     } 
     if (isEnderChest) {
       NMSUtil.setItemsFromInventoryBasic(NMSUtil.getNMSInventory(holder.inv), NMSUtil.getNMSInventory(player.getEnderChest()));
     } else {
       InvTrans.readInventory(holder.inv, (Inventory)player.getInventory());
     } 
   }
   
   public void onUpdateOnlineInv(Inventory inv, Player target, HumanEntity viewer) {
     OnlineInvHolder holder = (OnlineInvHolder)inv.getHolder();
     boolean isEnderChest = (holder.invType == InventoryType.EnderChest);
     if (isEnderChest)
       return; 
     InvTrans.writeInventory(holder.inv, (Inventory)holder.player.getInventory());
   }
 
   
   public void loadOfflineInv(Inventory inv, File file) throws IOException {
     boolean isEnderChest = (((OfflineInvHolder)inv.getHolder()).invType == InventoryType.EnderChest);
     NBTTagCompound nbt = NBTUtil.readNBTfromFile(file);
     NBTTagList nbtlist = (NBTTagList)nbt.get(isEnderChest ? "EnderItems" : "Inventory");
     if (isEnderChest) {
       int i; for (i = 0; i < inv.getSize(); i++)
         inv.setItem(i, null); 
       for (i = 0; i < nbtlist.size(); i++) {
         NBTTagCompound nbt1 = (NBTTagCompound)nbtlist.get(i);
         int j = ((NBTNumber)nbt1.get("Slot")).byteValue() & 0xFF;
         if (j >= 0 && j < inv.getSize())
           inv.setItem(j, nbt1.createStack()); 
       } 
     } else {
       for (int i = 0; i < nbtlist.size(); i++) {
         NBTTagCompound nbt1 = (NBTTagCompound)nbtlist.get(i);
         int j = ((NBTNumber)nbt1.get("Slot")).byteValue() & 0xFF;
         ItemStack itemstack = nbt1.createStack();
         if (itemstack != null && 
           InvTrans.canNBTToMyb(j)) {
           inv.setItem(InvTrans.fromNBTToMyb(j), itemstack);
         }
       } 
     } 
   }
 
   
   public void onCloseOfflineInv(Inventory inv, File file) throws IOException {
     boolean isEnderChest = (((OfflineInvHolder)inv.getHolder()).invType == InventoryType.EnderChest);
     NBTTagCompound nbt = NBTUtil.readNBTfromFile(file);
     NBTTagList nbtlist = new NBTTagList();
     ItemStack[] contents = inv.getContents();
     if (isEnderChest) {
       for (int i = 0; i < contents.length; i++) {
         if (contents[i] != null) {
           NBTTagCompound nbt1 = new NBTTagCompound();
           nbt1.set("Slot", (NBTBase)new NBTTagByte((byte)i));
           nbt1.saveStack(contents[i]);
           nbtlist.add((NBTBase)nbt1);
         } 
       } 
       nbt.set("EnderItems", (NBTBase)nbtlist);
     } else {
       for (int i = 0; i < contents.length; i++) {
         if (InvTrans.canMybToNBT(i) && contents[i] != null) {
           NBTTagCompound nbt1 = new NBTTagCompound();
           nbt1.set("Slot", (NBTBase)new NBTTagByte((byte)InvTrans.fromMybToNBT(i)));
           nbt1.saveStack(contents[i]);
           nbtlist.add((NBTBase)nbt1);
         } 
       } 
       nbt.set("Inventory", (NBTBase)nbtlist);
     } 
     File filetmp = new File(String.valueOf(file.getPath()) + ".tmp");
     FileOutputStream output = new FileOutputStream(filetmp);
     try {
       NBTCompressedStreamTools.write(nbt, output);
     } finally {
       output.close();
     } 
     if (file.exists())
       file.delete(); 
     filetmp.renameTo(file);
   }
   
   private void copyNBTValues(NBTTagCompound from, NBTTagCompound to) {
     if (this.cfgMan.multi_Health) {
       NBTUtil.copyValue("HealF", from, to);
       NBTUtil.copyValue("Health", from, to);
     } 
     if (this.cfgMan.multi_Attributes) {
       NBTUtil.copyValue("Attributes", from, to);
     }
     if (this.cfgMan.multi_Effects) {
       NBTUtil.copyValue("ActiveEffects", from, to);
     }
     if (this.cfgMan.multi_Inventory) {
       NBTUtil.copyValue("Inventory", from, to);
     }
     if (this.cfgMan.multi_Experience) {
       NBTUtil.copyValue("XpP", from, to);
       NBTUtil.copyValue("XpLevel", from, to);
       NBTUtil.copyValue("XpTotal", from, to);
     } 
     if (this.cfgMan.multi_FoodLevel) {
       NBTUtil.copyValue("foodLevel", from, to);
       NBTUtil.copyValue("foodTickTimer", from, to);
       NBTUtil.copyValue("foodSaturationLevel", from, to);
       NBTUtil.copyValue("foodExhaustionLevel", from, to);
     } 
     if (this.cfgMan.multi_Abilities) {
       NBTUtil.copyValue("abilities", from, to);
     }
     if (this.cfgMan.multi_EnderChest) {
       NBTUtil.copyValue("EnderItems", from, to);
     }
     if (this.cfgMan.multi_GameType) {
       NBTUtil.copyValue("playerGameType", from, to);
     }
     if (this.cfgMan.multi_Bukkit_ExtraData) {
       NBTUtil.copyValue("bukkit", from, to);
     }
   }
   
   private void clearNBTValues(NBTTagCompound nbt) {
     if (this.cfgMan.multi_Health) {
       nbt.remove("HealF");
       nbt.remove("Health");
     } 
     if (this.cfgMan.multi_Attributes) {
       nbt.remove("Attributes");
     }
     if (this.cfgMan.multi_Effects) {
       nbt.remove("ActiveEffects");
     }
     if (this.cfgMan.multi_Inventory) {
       nbt.remove("Inventory");
     }
     if (this.cfgMan.multi_Experience) {
       nbt.remove("XpP");
       nbt.remove("XpLevel");
       nbt.remove("XpTotal");
     } 
     if (this.cfgMan.multi_FoodLevel) {
       nbt.remove("foodLevel");
       nbt.remove("foodTickTimer");
       nbt.remove("foodSaturationLevel");
       nbt.remove("foodExhaustionLevel");
     } 
     if (this.cfgMan.multi_Abilities) {
       nbt.remove("abilities");
     }
     if (this.cfgMan.multi_EnderChest) {
       nbt.remove("EnderItems");
     }
     if (this.cfgMan.multi_GameType) {
       nbt.remove("playerGameType");
     }
     if (this.cfgMan.multi_Bukkit_ExtraData)
       nbt.remove("bukkit"); 
   }
 }


/* Location:              D:\User\Xuan\Downloads\MyBackpack1.2.5.jar!\net\saralab\mybackpack\core\compatible\VanillaHandler.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.1.3
 */